var app = getApp(), searchTitle = "";

Page({
  data: {
    types:1,
    page: 1,
    limit: 10,
    parductClassifyId:'',
    isFilterShow: !1,
    listmode: "block",
    listsort: "",
    page: 1,
    loaded: !1,
    loading: !0,
    opencategory: !1,
    category: [],
    category_child: [],
    filterBtns: {},
    isfilter: 0,
    category_child_selected: "",
    list: [],
    params: {},
    total: 0,
    fromsearch: !1,
    searchRecords: [],
    parductName:''
  },
  onLoad: function (t) {
    console.log(t);
    this.setData({
      id:t.id
    }) 
    t.fromsearch ? this.setData({
      fromsearch: t.fromsearch || !1
    }) : this.setData({
      params: t,
      fromsearch: t.fromsearch || !1
    }), 
    t.fromsearch || this.getList();
  },
  onShow: function () {
    searchTitle = "", this.data.fromsearch && this.setFocus();
  },
  onReachBottom: function () {
    this.data.loaded || this.getList();
  },
  getList: function () {
    console.log(this.page)
    var that = this
    app.getData({
      url: app.api.getSplbData(),
      method: 'POST',
      data: {
        page: that.data.page,
        limit: that.data.limit,
        type: that.data.types,
        parductClassifyId: that.data.id,
        price: that.data.price,
        parductSalesVolume: that.data.parductSalesVolume,
        isNew: that.data.isNew,
        parductName: that.data.parductName
      },
      ok: function (res) {
        console.log(res)
        
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            console.log(res.data.list);
            console.log(that.data.category)

            console.log(res.data.totals)
            if (that.data.category.length == res.data.totals) {
              console.log(123)
              that.setData({
                loading: !1,
              })
            } else {
              res.data.totals == null || res.data.list == "" || res.data.list.length < 1 ? that.setData({
                loading: !1,
              }) :
                res.data.list.length < 10 ? that.setData({
                  total:res.data.totals,
                  loading: !1,
                  category: that.data.category.concat(res.data.list)
                }) :
                  that.setData({
                    loading: !0,
                    total: res.data.totals,                    
                    page: that.data.page + 1,
                    category: that.data.category.concat(res.data.list)
                  });
            }

          } else {
            wx.showToast({
              title: res.data.message,
            })
          }
        }
      }

    })
  },
  changeMode: function () {
    "block" == this.data.listmode ? this.setData({
      listmode: ""
    }) : this.setData({
      listmode: "block"
    });
  },
  bindSort: function (t) {
    console.log(t.currentTarget.dataset.order)
    var a = t.currentTarget.dataset.order, e = this.data.params, price=null,parductSalesVolume=null,isNew=null;
    console.log(e)
    if ("" == a) {
      if (e.order == a) return;
      e.order = "", this.setData({
        listorder: ""
      }),isNew=1;
    } else if ("minprice" == a) this.setData({
      listorder: ""
    }), e.order == a ? "desc" == e.by ? (e.by = "asc", price = 0) : (e.by = "desc", price = 1) : (e.by = "asc", price = 0),
      e.order = a, this.setData({
        listorder: e.by
      }); else if ("sales" == a) {
        if (e.order == a) return;
        this.setData({
          listorder: ""
        }), parductSalesVolume=1,e.order = "sales", e.by = "desc";
      }
    this.setData({
      params: e,
      page: 1,
      category: [],
      loading: !0,
      loaded: !1,
      sort_selected: a,
      price: price,
      parductSalesVolume: parductSalesVolume,
      isNew: isNew,
    }), this.getList();
  },
  showFilter: function () {
    this.setData({
      isFilterShow: !this.data.isFilterShow
    });
  },
  bindFilterCancel: function () {
    this.setData({
      isFilterShow: !1,
      cateogry_parent_selected: "",
      category_child_selected: "",
      category_child: [],
      filterBtns: {},
      loaded: !1,
      listorder: ""
    });
  },
  bindFilterSubmit: function () {
    var t = this.data.params, a = this.data.category_child_selected;
    console.log(t, a), "" != a ? (t.type_id = a, t.name = "", this.setData({
      page: 1,
      params: t,
      isFilterShow: !1,
      list: [],
      loading: !0,
      loaded: !1
    }), this.getList()) : wx.showModal({
      title: "提示",
      content: "请选择子分类"
    });
  },
  bindCategoryEvents: function (t) {
    var a = t.target.dataset.index, e = t.target.dataset.id, s = this.data.params;
    console.log(a, e, s);
    var o = t.target.dataset.level;
    1 == o ? (this.setData({
      category_child: []
    }), this.setData({
      category_parent_selected: e,
      category_child_selected: "",
      category_child: this.data.category_parent[a].type2
    })) : 2 == o ? this.setData({
      category_child_selected: e
    }) : this.setData({});
  },
  bindSearch: function (t) {
    var a = this;
    console.log(a.data)
    if (console.log(searchTitle), "" != searchTitle) {
      var s = this.data.params;
      console.log(s)
      s.name = searchTitle, s.type_id = "", console.log(s), (a = this).setData({
        category: [],
        page: 1,
        parductName: s.name,
        id:s.id,
        params: s,
        listorder: "",
        fromsearch: !1,
        loading: !0,
        loaded: !1
      }), a.getList();
    } else wx.showToast({
      title: "搜索内容为空",
      icon: "loading",
      duration: 1e3
    });
  },
  bindInput: function (t) {
    console.log(t);
    "" != wx.getStorageSync("searchLog") ? this.setData({
      inputVal: t.detail.value,
      searchLogList: wx.getStorageSync("searchLog"),
      fromsearch: !0
    }) : this.setData({
      inputVal: t.detail.value,
      fromsearch: !0
    }), searchTitle = t.detail.value;
  },
  bindFocus: function (t) {
    "" == t.detail.value && this.setData({
      fromsearch: !0
    });
  },
  bindback: function () {
    wx.navigateBack();
  },
  searchDataByLog: function (t) {
    searchTitle = t.target.dataset.text, console.log(searchTitle), this.bindSearch();
  },
  delRecord: function () {
    wx.removeStorageSync("searchLog"), this.setData({
      searchLogList: []
    }), this.setData({
      fromsearch: !0
    });
  },
  setFocus: function () {
    var t = this;
    setTimeout(function () {
      t.setData({
        focusin: !0
      });
    }, 1e3);
  }
});